<?php

class Frmcreator_AdminManageController extends Core_Controller_Action_Admin {

    public function indexAction() {
        //
//        //Test URL
//        $test = Zend_Controller_Front::getInstance()->getRouter()->assemble(array('url' => 'form1'), 'frmcreator_view', true);
//        echo "<pre>". print_r($test, true). "</pre>";
//        die();
        
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->
                getNavigation('frmcreator_admin_main', array(), 'frmcreator_admin_main_manage');

        if ($this->getRequest()->isPost()) {
            $values = $this->getRequest()->getPost();
            foreach ($values as $key => $value) {
                if ($key == 'delete_' . $value) {
                    $form = Engine_Api::_()->getItem('frmcreator_form', $value);
                    $form->delete();
                }
            }
        }

        $page = $this->_getParam('page', 1);
        $this->view->paginator = Engine_Api::_()->getItemTable('frmcreator_form')->
                getFormsPaginator();
        $this->view->paginator->setItemCountPerPage(25);
        $this->view->paginator->setCurrentPageNumber($page);
    }

    public function deleteFormAction() {
        // In smoothbox
        $this->_helper->layout->setLayout('admin-simple');
        $id = $this->_getParam('id');
        $this->view->id = $id;
        $table = Engine_Api::_()->getDbtable('forms', 'frmcreator');
        $item = $table->find($id)->current();

        if (!$item) {
            return $this->_forward('success', 'utility', 'core', array(
                        'smoothboxClose' => 10,
                        'parentRefresh' => 10,
                        'messages' => array('')));
        } else {
            $id = $item->getIdentity();
            
        }

        if (!$this->getRequest()->isPost()) {
            // Output
            $this->renderScript('admin-manage/delete.tpl');
            return;
        }

        // Process
        $db = $table->getAdapter();
        $db->beginTransaction();

        try {
             // remove menu items
            $table->removeMenuItem($id);

            $item->delete();
            $db->commit();
            
        } catch (exception $e) {
            $db->rollBack();
            throw $e;
        }

        return $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 10,
                    'parentRefresh' => 10,
                    'messages' => array('You have been deleted successfully!')));
    }

    public function addFormAction() {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->
                getNavigation('frmcreator_admin_main', array(), 'frmcreator_admin_main_manage');


        $elementTable = Engine_Api::_()->getDbtable('elements', 'frmcreator');
        $formTable = Engine_Api::_()->getDbtable('forms', 'frmcreator');

        if ($this->getRequest()->isPost()) {

            // insert to table engine4_frmcreator_forms 
            $formdata = $this->_getParam('form_settings');
            $form_id = $formTable->createForm($formdata);

            // insert to table engine4_frmcreator_elements 
            $data = $this->_getParam('form_data', array());
            $elementTable->createElement($data, $form_id);
            exit();
        }
    }

    public function editFormAction() {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->
                getNavigation('frmcreator_admin_main', array(), 'frmcreator_admin_main_manage');
        
        $elementTable   = Engine_Api::_()->getDbtable('elements', 'frmcreator');
        $formTable      = Engine_Api::_()->getDbtable('forms', 'frmcreator');
        $optionTable    = Engine_Api::_()->getDbtable('options', 'frmcreator');
      
        $this->view->form_id   = $form_id = $this->_getParam('id', null);

        if ($this->getRequest()->isPost() && $form_id ) { 
            //remove options 
            $optionTable->removeOptions($form_id); 
            //remove old elements 
            $elementTable->delete($elementTable->getAdapter()->quoteInto('form_id = ?', $form_id));
            
            // update form settings 
            $formdata = $this->_getParam('form_settings');
            $formTable->updateForm($formdata, $form_id);

            // insert new data to table engine4_frmcreator_elements 
            $data = $this->_getParam('form_data', array());
            $elementTable->createElement($data, $form_id);
            exit();
        }
        
        
        //fetch data ! 
        $form   = $formTable->fetchRow($formTable->select()->where('form_id = ?', $form_id));
        
        if($form){
            $this->view->form = $form->toArray();    
        } else {
           return $this->_forward('requireauth', 'error', 'core');
        }
      
        if( !$form_id || !$form ) {
            return $this->_helper->redirector->gotoRoute(array('action' => 'index'));
        }
   
        $this->view->elements   = $elementTable->getElement($form_id);
    }
    
    public function activeFormAction(){
        // In smoothbox
        $this->_helper->layout->setLayout('admin-simple');
        $id = $this->_getParam('id');
        $this->view->id = $id;
        $table = Engine_Api::_()->getDbtable('forms', 'frmcreator');
        $item = $table->find($id)->current();

        if (!$item) {
            return $this->_forward('success', 'utility', 'core', array(
                        'smoothboxClose' => 10,
                        'parentRefresh' => 10,
                        'messages' => array('The finding form is not exist!')));
        } else {
            $id = $item->getIdentity();
            
        }

        // Process
        $db = $table->getAdapter();
        $db->beginTransaction();

        try {
             // remove menu items
             $item->status = 1;

            $item->save();
            $db->commit();
            
            $url = $item->url;
            
            if(!empty($url)){               
                $params = array(
                    'name' => 'frmcreator_footer_' . $url,
                    'module' => 'frmcreator',
                    'label' => $item->title,
                    'plugin' => '',
                    'params' => '{"route":"frmcreator_view","action":"' . $url . '"}',
                    'menu' => 'core_footer',
                    'submenu' => '',
                    'enabled' => 1,
                    'custom' => 0,
                    'order' => 999
                );            
                Engine_Api::_()->frmcreator()->getFormFooterMenu($params, true);
            }
        } catch (exception $e) {
            $db->rollBack();
            throw $e;
        }

        return $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 10,
                    'parentRefresh' => 10,
                    'messages' => array('Your form has been enable successfully!')));
        
    }
    
    public function inactiveFormAction(){
        // In smoothbox
        $this->_helper->layout->setLayout('admin-simple');
        $id = $this->_getParam('id');
        $this->view->id = $id;
        $table = Engine_Api::_()->getDbtable('forms', 'frmcreator');
        $item = $table->find($id)->current();

        if (!$item) {
            return $this->_forward('success', 'utility', 'core', array(
                        'smoothboxClose' => 10,
                        'parentRefresh' => 10,
                        'messages' => array('The finding form is not exist!')));
        } else {
            $id = $item->getIdentity();
            
        }

        // Process
        $db = $table->getAdapter();
        $db->beginTransaction();

        try {
             // remove menu items
            $item->status = 0;

            $item->save();
            $db->commit();
                        
            $url = $item->url;
            $menuitems = Engine_Api::_()->getDbtable('menuItems', 'core');
            $select = $menuitems->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                    ->setIntegrityCheck(false)
                    ->where("name = 'frmcreator_footer_$url'");
            $result = $menuitems->fetchRow($select);        
            
            if($result){
                //Remove Menu out Footer
                $result->delete();
            }
            
            
        } catch (exception $e) {
            $db->rollBack();
            throw $e;
        }

        return $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 10,
                    'parentRefresh' => 10,
                    'messages' => array('Your form has been disable successfully!')));
    }

}
